// filepath: /home/debi/IdeaProjects/LearnRust/Work/basic/src/corex/hashx/hashx.rs
// stdx/hashx.rs
// CRITICAL: Hash trait 与哈希相关示例。
// AUTO_COMMENT: 展示如何实现 Hash trait 以及使用哈希集合。

#[allow(dead_code)]
pub fn stdx_examples_hashx() {
    use std::collections::HashMap;
    use std::hash::{Hash, Hasher};

    // CRITICAL: 基本 HashMap 使用
    let mut map = HashMap::new();
    map.insert("key1", 1);
    map.insert("key2", 2);
    let value = map.get("key1");

    // CRITICAL: 自定义类型实现 Hash
    #[derive(Debug, Eq, PartialEq)]
    struct Person {
        name: String,
        age: u32,
    }

    impl Hash for Person {
        fn hash<H: Hasher>(&self, state: &mut H) {
            self.name.hash(state);
            self.age.hash(state);
        }
    }

    let mut people_map: HashMap<Person, String> = HashMap::new();
    let person = Person {
        name: "Alice".to_string(),
        age: 30,
    };
    people_map.insert(person, "Engineer".to_string());

    // CRITICAL: HashSet 使用
    use std::collections::HashSet;
    let mut set: HashSet<i32> = HashSet::new();
    set.insert(1);
    set.insert(2);
    let contains = set.contains(&1);

    let _ = (value, people_map, contains);
}
